<?php
	include_once 'isLogin.php';	
	$table_name = "companies";
	$table_detail = "companies_detail";
	$table_question = "company_levequestion";
	$table_categories="companies_categories";
	$table_grade="company_grade";
			
	//查询一级分类id
	if($_GET['id']!="" && $_GET['id']!=null){
	$sql=$table_name." c left join ".$table_categories." g on c.upid=g.sort where c.id=".$_GET['id'];
	$result=$commonc->getList($sql, $sqlnum='', $key="g.upid",$debug='0');
	foreach($result['result'] as $key=>$value){
	}
	$a=$value['upid'];
	$sql1 = $table_question.' as q where q.categories_id='.$value['upid']." order by q.list_id ";
	$row=$commonc->getList($sql1, $sqlnum='', $key="q.conent,q.list_id,q.id,q.categories_id",$debug='0');
	//添加
	}
	//将评级问题列出
	function get_array($id=0){
            $sql = "select list_id,conent,id,categories_id from company_levequestion where title_id=".$id." order by list_id";;
			$result = mysql_query($sql);//查询子类 
			$arr = array(); 
			if($result && mysql_affected_rows()){//如果有子类 
				while($rows=mysql_fetch_assoc($result)){ //循环记录集 
					$rows['list'] = get_array($rows['id']); //调用函数，传入参数，继续查询下级 
					$arr[] = $rows; //组合数组 
				} 
				return $arr; 
			} 
		}
		$cate = get_array(0); //调用函数
	
		
		
	if($_GET['action']=='add'){
		
		//插入数据
		if(is_array($_POST['values'])){
			foreach($_POST['values'] as $k=>$v){
				$arr[$key_index[$k]] = $v;
			}
		}
		$commonc->insert($table_name,$arr);
		$id = $commonc->insert_id();
		//排序
		$arr = array('sort'=>$id);
		$commonc->update($table_name, $arr, "id='".$id."'");
		
		//查询记录
		$data = $commonc->getInfo($table_name, $id);
	}
	
	//提交
	if($_POST['submit']){
		$score=array();
		if($_POST['score']!="" && $_POST['score']!=null){
			$score=$_POST['score'];
			foreach($score as $key=>$value){
				
			}
	    if(in_array('0',$score)){
			$sum=0;
		}else{
		//计算出评分的总分
		$sum=array_sum($score);
		//计算出总共有几个打分项目
		$num=count(array_filter($score));
		//计算出平均分
		if($sum!=0){
		$avg=$sum/$num;
		}
		//输出分数所生成的星级
			if($avg>=80&& $avg<=100){
			    $star=5;
			}elseif($avg>=60&& $avg<80){
				$star=4;
			}elseif($avg>=40&& $avg<60){
				$star=3;
			}elseif($avg>=20&& $avg<40){
				$star=2;
			}elseif($avg>=0&& $avg<20){
				$star=1;
			}
		}
		//插入数据库
		//查询公司所属那个分类
			$sql=$table_name." c left join ".$table_categories." g on c.upid=g.sort where c.id=".$_GET['id'];
			$result=$commonc->getList($sql, $sqlnum='', $key="g.upid",$debug='0');
				foreach($result['result'] as $key=>$value){
				}
		
			$data=array('company_id'=>$_GET['id'],'categories_id'=>$value['upid'],'star'=>$star,'sumscore'=>$sum,'averagescore'=>$avg,'num'=>$num);
            $datas=array('star'=>$star);				
		    $commonc->insert($table_grade,$data);
			$commonc->update($table_name, $datas, "id='".$_GET['id']."'");
			$href='';
	    }
		echo $commonc->submitAlert('成功',$href);
	}
	//获取打分的数据
	if($_GET['id']!='' && $_GET['id']!=null){
	$sql1 = $table_grade." where company_id=".$_GET['id']." order by id DESC limit 0,1";
	$grade=$commonc->getList($sql1, $sqlnum='', $key="*",$debug='0');
	if($grade['result']!='' && $grade['result']!=null)
	foreach($grade['result'] as $grades){
	}
	}
	$smarty->assign("grade",$grades);
	$smarty->assign("sum",$sum);
	$smarty->assign("avg",$avg);
	$smarty->assign("num",$num);
	$smarty->assign("cate",$cate);
	$smarty->assign("categories",$value['upid']);
	$smarty->assign("upid",$upid);
	$smarty->assign("row",$row['result']);
	$smarty->assign('page',$row['page']);
	$smarty->display($_filename.".html");
?>